﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.Entities;

namespace Basic4Net.BusinessRules
{
    public class TeacherInfo
    {
        public bool ExistTeacher(int TeacherID)
        {
            using (DBEntities db = new DBEntities())
            {
                var obj = (from t in db.TEACHERINFO
                           where t.RID == TeacherID
                           select t).Count();
                if (obj > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public TEACHERINFO GetBaseTeacher(int TeacherID)
        {
            using (DBEntities db = new DBEntities())
            {
                var obj = (from t in db.TEACHERINFO
                           where t.RID == TeacherID
                           select t).FirstOrDefault();
                return obj;
            }
        }

        public ListTeacherInfo GetTeacher(int TeacherID)
        {
            using (DBEntities db = new DBEntities())
            {
                var obj = (from a in db.TEACHERINFO
                           join b in db.t_PostRelation on a.RID equals b.TeacherID into temp1
                           from c in temp1.DefaultIfEmpty()
                           join d in db.t_Post on c.PostID equals d.RID into temp2
                           from e in temp2.DefaultIfEmpty()
                           join f in db.t_PositionRelation on a.RID equals f.TeacherID into temp3
                           from g in temp3.DefaultIfEmpty()
                           join h in db.t_Position on g.PositionID equals h.RID into temp4
                           from i in temp4.DefaultIfEmpty()
                           join j in db.t_TeacherRelation on a.RID equals j.TeacherID into temp5
                           from k in temp5.DefaultIfEmpty()
                           join l in db.t_TeacherType on k.TypeID equals l.RID into temp6
                           from t in temp6
                           where a.RID == TeacherID
                           select new ListTeacherInfo
                           {
                               RID = t.RID,
                               TEACHCODE = a.TEACHCODE,
                               TEACHNAME = a.TEACHNAME,
                               SEXY = a.SEXY != null ? (int)a.SEXY : 0,
                               BIRTHDAY = a.BIRTHDAY != null ? (DateTime)a.BIRTHDAY : DateTime.MaxValue,
                               INSCHOOLDATE = a.INSCHOOLDATE != null ? (DateTime)a.INSCHOOLDATE : DateTime.MaxValue,
                               SCHOOLMARK = a.SCHOOLMARK,
                               TEACHNAMEPINYIN = a.TEACHNAMEPINYIN,
                               POSTID = e.RID != null ? e.RID : -1,
                               POSTNAME = e.PostName != null ? e.PostName : "尚未分配",
                               POSITIONID = i.RID != null ? i.RID : -1,
                               POSITIONNAME = i.FName != null ? i.FName : "尚未分配",
                               TYPEID = t.RID != null ? t.RID : -1,
                               TYPENAME = t.TypeName != null ? t.TypeName : "尚未分配"
                           }).FirstOrDefault();
                return obj;
            }
        }

        public bool ExistPermit(int TeacherID, int permit)
        {
            using (DBEntities db = new DBEntities())
            {
                var super = (from t in db.t_Permit
                             where t.TeacherID == TeacherID
                             && t.Super == 1
                             select t).Count();
                if (super > 0)
                    return true;
                else
                {
                    var obj = (from t in db.t_Permit
                               where t.TeacherID == TeacherID
                               && t.FType == permit
                               select t).Count();
                    if (obj > 0)
                        return true;
                    else
                        return false;
                }
            }
        }

        public bool ExistSuper(int TeacherID)
        {
            using (DBEntities db = new DBEntities())
            {
                var super = (from t in db.t_Permit
                             where t.TeacherID == TeacherID
                             && t.Super == 1
                             select t).Count();
                if (super > 0)
                    return true;
                else
                    return false;
            }
        }

        public string GetPermit(int TeacherID)
        {
            using (DBEntities db = new DBEntities())
            {
                StringBuilder str = new StringBuilder();
                var super = (from t in db.t_Permit
                             where t.TeacherID == TeacherID
                             orderby t.FType, t.Super
                             select t).ToList();
                foreach (t_Permit model in super)
                {
                    switch (model.FType)
                    {
                        case 0:
                            if (model.Super == 1)
                            {
                                str.Append("超级管理员");
                            }
                            break;
                        case 1:
                            str.Append("基础数据管理");
                            break;
                        case 2:
                            str.Append("流程管理");
                            str.Append("、");
                            break;
                        case 3:
                            str.Append("拨款管理");
                            break;
                        case 4:
                            str.Append("工作量管理");
                            break;
                        case 5:
                            str.Append("考勤管理");
                            break;
                        case 6:
                            str.Append("核算管理");
                            break;
                    }
                    str.Append("、");
                }
                str.Append("分析报表");
                return str.ToString();
            }
        }

        public string GetPost(int TeacherID)
        {
            using (DBEntities db = new DBEntities())
            {
                StringBuilder str = new StringBuilder();
                var obj = (from a in db.t_Post
                           from b in db.t_PostRelation
                           where a.RID == b.PostID
                           && b.TeacherID == TeacherID
                           orderby a.RID
                           select new
                           {
                               PostName = a.PostName
                           }).ToList();
                for (int i = 0; i < obj.Count(); i++)
                {
                    str.Append(obj[i].PostName);
                    str.Append("、");
                }
                if (obj.Count > 0)
                    return Basic4Net.Utility.Common.Common.DelStrLast(str.ToString(), "、");
                else
                    return "尚未分配";
            }
        }
    }
    public class ListTeacherInfo
    {
        private int _rid;
        private string _teachcode;
        private string _teachname;
        private int _sexy;
        private DateTime _birthday;
        private DateTime _inschooldate;
        private string _schoolmark;
        private string _teachnamepinyin;
        private int _postid;
        private string _postname;
        private int _positionid;
        private string _positionname;
        private int _typeid;
        private string _typename;

        public int RID
        {
            get { return _rid; }
            set { _rid = value; }
        }

        public string TEACHCODE
        {
            get { return _teachcode; }
            set { _teachcode = value; }
        }

        public string TEACHNAME
        {
            get { return _teachname; }
            set { _teachname = value; }
        }

        public int SEXY
        {
            get { return _sexy; }
            set { _sexy = value; }
        }

        public DateTime BIRTHDAY
        {
            get { return _birthday; }
            set { _birthday = value; }
        }

        public DateTime INSCHOOLDATE
        {
            get { return _inschooldate; }
            set { _inschooldate = value; }
        }

        public string SCHOOLMARK
        {
            get { return _schoolmark; }
            set { _schoolmark = value; }
        }

        public string TEACHNAMEPINYIN
        {
            get { return _teachnamepinyin; }
            set { _teachnamepinyin = value; }
        }

        public int POSTID
        {
            get { return _postid; }
            set { _postid = value; }
        }

        public string POSTNAME
        {
            get { return _postname; }
            set { _postname = value; }
        }

        public int POSITIONID
        {
            get { return _positionid; }
            set { _positionid = value; }
        }

        public string POSITIONNAME
        {
            get { return _positionname; }
            set { _positionname = value; }
        }

        public int TYPEID
        {
            get { return _typeid; }
            set { _typeid = value; }
        }

        public string TYPENAME
        {
            get { return _typename; }
            set { _typename = value; }
        }
    }
}
